---------------------------------------------------
-- 充值-每日礼包
-- 每日0点刷新购买次数

-- db.dailyLibao[id] = cnt 
---------------------------------------------------

local DailyLibaoExcel = require("excel.present").dailyLibao
local Lang = require("common.Lang")
local Msg = require("core.Msg")
local ObjHuman = require("core.ObjHuman")
local Broadcast = require("broadcast.Broadcast")
local PanelDefine = require("broadcast.PanelDefine")
local Grid = require("bag.Grid")
local BagLogic = require("bag.BagLogic")
local BuyLogic = require("topup.BuyLogic")
local VipLogic = require("vip.VipLogic")
local YunYingLogic = require("yunying.YunYingLogic")

local FREE_LIBAO_ID = 0 		-- 免费
local QUICK_LIBAO_ID = 1  		-- 一键购买

local QUICK_STATUS_CANT = 0 	-- 不能购买
local QUICK_STATUS_CAN = 1 		-- 可购买

-- 已购买数量
function getCnt(human, id)
	if not human.db.dailyLibao then
		return 0
	end
	return human.db.dailyLibao[id] or 0
end

function addCnt(human, id)
	if not human.db.dailyLibao then
		human.db.dailyLibao = {}
	end
	human.db.dailyLibao[id] = getCnt(human, id) + 1 
end

-- 剩余次数
function getLeftCnt(human, id)
	local config = DailyLibaoExcel[id]
	if not config then return 0 end

	return math.max(config.cnt - getCnt(human, id), 0)
end

-- 封装礼包结构体
function fontLibaoNet(net, id, config, human)
	net.id = id 
	net.items[0] = #config.items
	net.doubleFlags[0] = net.items[0]
	for i = 1, net.items[0] do
		local itemID = config.items[i][1]
		local itemCnt = config.items[i][2]
		Grid.makeItem(net.items[i], itemID, itemCnt)
		net.doubleFlags[i] = config.items[i][3] or 0  -- 是否显示双倍标签
	end
	net.cnt = getCnt(human, id)
	net.maxCnt = config.cnt
	net.vipLv = 0
	BuyLogic.fontBuyItem(human, net.buyItem, config.buyID)
	net.name = config.name
end

-- 可否一键购买 
local function canQuickBuy(human)
	for id, config in ipairs(DailyLibaoExcel) do
		if id ~= QUICK_LIBAO_ID then
			-- 剩余购买次数
			if getCnt(human, id) >= config.cnt then
				return 
			end
		end
	end
	return true
end

-- 获取原价
local function getQuickOldPrice(human)
	local sum = 0
	for id, config in ipairs(DailyLibaoExcel) do
		if id ~= QUICK_LIBAO_ID then
			sum = sum + BuyLogic.getRealPrice(human, config.buyID)
		end
	end
	return sum
end

-- 封装一键购买信息
function fontQuickNet(net, human)
	local config = DailyLibaoExcel[QUICK_LIBAO_ID]
	net.status = canQuickBuy(human) and QUICK_STATUS_CAN or QUICK_STATUS_CANT
	net.oldPrice = getQuickOldPrice(human)
	net.vipLv = 0
	BuyLogic.fontBuyItem(human, net.buyItem, config.buyID)
end

-- 面板查询
function sendQuery(human)
	ObjHuman.updateDaily(human)
	local msgRet = Msg.gc.GC_PRE_DAILY_LIBAO_QUERY
	msgRet.list[0] = 0
	for id, config in ipairs(DailyLibaoExcel) do
		if id ~= QUICK_LIBAO_ID then
			msgRet.list[0] = msgRet.list[0] + 1
			fontLibaoNet(msgRet.list[msgRet.list[0]], id, config, human)
		end
	end
	fontQuickNet(msgRet.quickData, human)
	msgRet.freeRed = (getLeftCnt(human, FREE_LIBAO_ID) > 0) and 1 or 0
	--Msg.trace(msgRet)
	Msg.send(msgRet, human.fd)
end

-- 给予东西
local function giveLibaoItems(human, id)
	local config = DailyLibaoExcel[id]
	if not config then return end
	if getLeftCnt(human, id) < 1 then
		return Broadcast.sendErr(human, Lang.YUNYING_BUY_ERR_CNT)
	end
	
	addCnt(human, id)
	return config.items
end

-- 领取免费礼包
function getPreFreeLibao(human)
	ObjHuman.updateDaily(human)
	local items = giveLibaoItems(human, FREE_LIBAO_ID)
	if not items then return end
	sendQuery(human)
	-- 通用获得道具弹窗
    if items then
        for _, item in pairs(items) do
            BagLogic.addItem(human, item[1], item[2], "daily_libao")
        end
    end
	BagLogic.sendItemGetList(human, items, "daily_libao")
    for k, v in pairs(funcID) do
        YunYingLogic.updateIcon(YYInfo[k], human)
        YunYingLogic.sendGroupUpdate(YYInfo[k], human, PanelDefine.PANEL_ID_3107)
        break
    end
end

-- 购买礼包
function buyLibao(human, id)
	ObjHuman.updateDaily(human)
	local itemsAll = nil
	if id == QUICK_LIBAO_ID then
		for tid, config in ipairs(DailyLibaoExcel) do
			if tid ~= QUICK_LIBAO_ID then
				local items = giveLibaoItems(human, tid)
				if items then
					itemsAll = itemsAll or {}
					for _, item in ipairs(items) do
						itemsAll[#itemsAll + 1] = item
					end
				end
			end
		end	
		itemsAll = BagLogic.sameItemTogether(itemsAll)	
	else
		itemsAll = giveLibaoItems(human, id)
	end
	if not itemsAll then return end

    if itemsAll then
        for _, item in pairs(itemsAll) do
            BagLogic.addItem(human, item[1], item[2], "mail")
        end
    end
	-- 通用获得道具弹窗
	BagLogic.sendItemGetList(human, itemsAll, "daily_libao")

    sendQuery(human)
end

-- 红点
function isRed(human)
	-- 有免费礼包可以领取
	if getLeftCnt(human, FREE_LIBAO_ID) > 0 then
		return true
	end
end